﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using handvn.Models;

namespace handvn.Controllers
{ 
    public class DetailCategoriesController : Controller
    {
        private HandvnContext db = new HandvnContext();

        //
        // GET: /DetailCategories/

        public ViewResult Index()
        {
            var detailcategories = db.DetailCategories.Include(d => d.Categories);
            return View(detailcategories.ToList());
        }

        //
        // GET: /DetailCategories/
        public ViewResult View(int cateid)
        {
            var detailcategory = db.DetailCategories.Where(d => d.CategoriesID == cateid);
            //return View(detailcategory.ToList());
            var products = db.Products.Where(p => p.DetailCategories.CategoriesID == cateid);
            var model = new DetailCategoriesView
            {
                Product = products.ToList(),
                DetailCategory = detailcategory.ToList()
            };
            return View(model);
        }
        //
        // GET: /DetailCategories/Details/5

        public ViewResult Details(int id)
        {
            DetailCategories detailcategories = db.DetailCategories.Find(id);
            return View(detailcategories);
        }

        //
        // GET: /DetailCategories/Create

        public ActionResult Create()
        {
            ViewBag.CategoriesID = new SelectList(db.Categories, "CategoriesID", "TenDanhMuc");
            return View();
        } 

        //
        // POST: /DetailCategories/Create

        [HttpPost]
        public ActionResult Create(DetailCategories detailcategories)
        {
            if (ModelState.IsValid)
            {
                db.DetailCategories.Add(detailcategories);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.CategoriesID = new SelectList(db.Categories, "CategoriesID", "TenDanhMuc", detailcategories.CategoriesID);
            return View(detailcategories);
        }
        
        //
        // GET: /DetailCategories/Edit/5
 
        public ActionResult Edit(int id)
        {
            DetailCategories detailcategories = db.DetailCategories.Find(id);
            ViewBag.CategoriesID = new SelectList(db.Categories, "CategoriesID", "TenDanhMuc", detailcategories.CategoriesID);
            return View(detailcategories);
        }

        //
        // POST: /DetailCategories/Edit/5

        [HttpPost]
        public ActionResult Edit(DetailCategories detailcategories)
        {
            if (ModelState.IsValid)
            {
                db.Entry(detailcategories).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CategoriesID = new SelectList(db.Categories, "CategoriesID", "TenDanhMuc", detailcategories.CategoriesID);
            return View(detailcategories);
        }

        //
        // GET: /DetailCategories/Delete/5
 
        public ActionResult Delete(int id)
        {
            DetailCategories detailcategories = db.DetailCategories.Find(id);
            return View(detailcategories);
        }

        //
        // POST: /DetailCategories/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            DetailCategories detailcategories = db.DetailCategories.Find(id);
            db.DetailCategories.Remove(detailcategories);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}